<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form lay-filter="formMenu" class="layui-form layuimini-form">
    <input name="parentId" type="hidden" value="">
    <div class="layui-form-item">
        <label class="layui-form-label required">父菜单名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="parentName" id="parentName" disabled="">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" name="name" lay-verify="required" placeholder="请输入菜单名称">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单地址</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input"  name="href"  placeholder="请输入菜单地址">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">菜单权限</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" lay-verify="required" name="permission"  placeholder="菜单权限">
        </div>
    </div>

    <div class="layui-form-item">
        <label for="" class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" name="icon" id="iconPicker" lay-filter="iconPicker" class="hide">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单类型</label>
        <div class="layui-input-block">
            <input type="radio" name="isMenu" value="0" title="菜单" checked="">
            <input type="radio" name="isMenu" value="1" title="按钮">
            <input type="radio" name="isMenu" value="-1" title="目录">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addChailMenu">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../api/baseApi.js" charset="utf-8"></script>
<script src="../../api/menusApi.js" charset="utf-8"></script>
<script>
    layui.use(['form','iconPickerFa'], function () {
        let iconPickerFa = layui.iconPickerFa,
            form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 获取token
        const token = localStorage.getItem('token');
        //ajax全局参数设置
        $.ajaxSetup({
            headers:{
                Authorization: 'Bearer '+token
            },
            // 发送cookie
            xhrFields: {
                withCredentials: true
            }
        });

        //获取列表页传过来的菜单数据
        let localData = layui.sessionData('childMenuData');

        form.val('formMenu',{
            parentId:localData.childMenu.id,
            parentName:localData.childMenu.name
        });

        //选择图标
        iconPickerFa.render({
            // 选择器，推荐使用input
            elem: '#iconPicker',
            // fa 图标接口
            url: "../../lib/font-awesome-4.7.0/less/variables.less",
            // 是否开启搜索：true/false，默认true
            search: true,
            // 是否开启分页：true/false，默认true
            page: true,
            // 每页显示数量，默认12
            limit: 24,
            // 点击回调
            click: function (data) {
                //给表单赋值
                form.val("formMenu",{
                    icon:data.icon
                });
                console.log(data);
            },
            // 渲染成功后的回调
            success: function (d) {
                console.log(d);
            }
        });

        //监听提交
        form.on('submit(addChailMenu)', function (data) {
            let loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            $.post({
                url: addUrl,
                data:data.field,
                dataType: "json",
                timeout:300000,
                xhrFields:{withCredentials: true},
                success: function (res) {
                    layer.close(loadIndex);
                    if(res.success){
                        parent.layer.msg("菜单添加成功！",{time:1000},function(){
                            //刷新父页面
                            parent.location.reload();
                        });
                    }else{
                        layer.msg(res.message);
                    }
                },
                error: function () {

                }

            });


            return false;
        });

    });
</script>
</body>
</html>
